﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlanB.Core.Logging
{

    /// <summary>
    /// This static class provides methods for easier creating and handling logging tasks
    /// </summary>
    public static class LogFactory
    {
        /// <summary>
        /// Creates a instance of a iLogger implementationion, based on the LoggingConfiguration
        /// </summary>
        /// <param name="configurationSection">Configuration section with log settings</param>
        /// <returns>Implementation og iLogger instance</returns>
        public static ILogger InstantiateLog(PlanB.Core.Logging.LoggingConfiguration configurationSection)
        {
            if (configurationSection != null)
            {
                System.Reflection.Assembly assembly = PlanB.Core.Helpers.ReflectionHelpers.GetAssembly(configurationSection.LoggingImplementation.Type);

                //
                //instantiate class if the assembly has been properly loaded
                if (assembly != null)
                {
                    string loggerType = configurationSection.LoggingImplementation.Type.Split(',')[0].Trim();
                    object instance = assembly.CreateInstance(loggerType);

                    //
                    // check if this class implements ILandingPageProvider interface
                    if (instance is ILogger)
                    {
                        return (ILogger)instance;
                    }
                    else
                    {
                        //TODO: change exception
                        throw new ApplicationException("Class instance does not implement the business layer interface.");
                    }
                }
                else
                {
                    //TODO: change exception
                    throw new ApplicationException("Could not load business layer assembly.");
                }
            }

            //TODO: CURRENT POSITION
            throw new Exception("Not yet finished");
        }

    }
}
